<?php

require_once __DIR__."/core/CBase.php";

class lighter extends CBase
{
	
	function __construct($config=NULL)
	{
		parent::__construct();
		
		$this->init($config);
	}
	
	private function init($config)
	{
		spl_autoload_register(array('lighter','autoload'));
		if(!isset($_SESSION))
		{
			if(strtolower($_SERVER["HTTP_USER_AGENT"])=="shockwave flash")
			{
				$sessionId = isset($_POST['PHPSESSID'])?$_POST['PHPSESSID']:'';
				if(!empty($sessionId)){
					session_id($_POST['PHPSESSID']);
				}
			}
			session_start();
		}
		Config::init($config);
		
		$timezone = Config::get("timezone","");
		if($timezone)
		{
			date_default_timezone_set($timezone);
		}

		set_exception_handler(array("CException","handler"));
		
		set_error_handler(array('CError', 'handler'),E_ALL);
		
		register_shutdown_function(array('CError', 'shutdown'));
		
		$this->setUserAutoLoadClass();
	}
	
	public function run()
	{
		$request = new CRequest();
		$request->exec();
	}
	
	private function setUserAutoLoadClass()
	{
		$autoLoadConfig = Config::get("autoLoad.file","");
		if(!$autoLoadConfig)
		{
			return;
		}
		
		if(is_array($autoLoadConfig))
		{
			foreach($autoLoadConfig as $k=>$item)
			{
				if(!key_exists($k,$this->_lighterClass))
				{
					if(is_file($item))
					{
						$this->_lighterClass[$k] = $item;
					}
					else
					{
						if(is_file($this->getAbsolutePath($item)))
						{
							$this->_lighterClass[$k] = $this->getAbsolutePath($item);
						}
					}
				}
			}
		}
	}
	
	public function autoload($className)
	{
		if(isset($this->_lighterClass[$className]))
		{
			if(is_file($this->_lighterClass[$className]))
			{
				include_once $this->_lighterClass[$className];
			}
			else if(is_file(__DIR__.$this->_lighterClass[$className]))
			{
				include_once __DIR__.$this->_lighterClass[$className];
			}
			else{
				CError::handler(E_ERROR, "文件".$this->_lighterClass[$className]."不存在",__FILE__,__LINE__);
			}
			return;
		}
		
		$autoLoadConfig = Config::get("autoLoad","");
		if(!$autoLoadConfig)
		{
			return;
		}

		if(!is_array($autoLoadConfig))
		{
			CError::handler(E_ERROR, "自动加载配置错误",__FILE__,__LINE__);
		}

		if(isset($autoLoadConfig["dir"]))
		{
			$dirAutoLoad = $autoLoadConfig["dir"];
			$dirItem = "";
			
			foreach ($dirAutoLoad as $item)
			{
				$dirItem = "";
				if(is_string($item))
				{
					if(is_dir($item))
					{
						$dirItem = $item;
					}
					else{
						$item = $this->getAbsolutePath($item);
						
						if(is_dir($item))
						{
							$dirItem = $item;
						}
					}
					
					if($dirItem)
					{
						if(is_file($dirItem."/".$className.".php"))
						{
							include_once $dirItem."/".$className.".php";
							return;
						}
					}
				}
			}
		}
	}
	
	private $_lighterClass = array(
		"CBase"=>"/core/CBase.php",
		"CRequest"=>"/core/CRequest.php",
		"Config"=>"/core/Config.php",
		"CException"=>"/core/CException.php",
		"CError"=>"/core/CError.php",
		"CLog"=>"/core/CLog.php",
		"CController"=>"/core/CController.php",
		"CView"=>"/core/CView.php",
		"CViewSmarty"=>"/core/CViewSmarty.php",
		"CValidatorManage"=>"/core/CFormValidator.php",
		"CFormValidator"=>"/core/CFormValidator.php",
		"CUtil"=>"/core/CUtil.php",
		"CDBModel"=>"/core/CDBModel.php",
		"CLog"=>"/core/CLog.php",
		"CPageBase"=>"/core/CPageBase.php",
		"CPageDefault"=>"/core/CPageDefault.php",
		"CPage"=>"/core/CPage.php",
		"CLogFile"=>"/core/CLogFile.php",
		"CDBBase"=>"/core/db/CDBBase.php",
		"CMysql"=>"/core/db/CMysql.php",
		"IDatabase"=>"/core/interface/IDatabase.php",
		"IView"=>"/core/interface/IView.php",
		"ILog"=>"/core/interface/ILog.php",
		"COracle"=>"/core/db/COracle.php",
	);
}

